package servlet.admin;

import net.sf.json.JSONObject;
import service.admin.BookSortService;
import service.admin.impl.BookSortServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * BookSortDelServlet
 *
 * @author 谭文斌
 * @since 2023/9/5
 */
@WebServlet("/admin/bookSortDelServlet")
public class BookSortDelServlet extends HttpServlet {
    BookSortService service = new BookSortServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf8");
        JSONObject jsonObject = new JSONObject();
        PrintWriter out = resp.getWriter();

        String id = req.getParameter("id");

        // 准备返回数据
        int code = 1;
        String msg = "";

        int del = service.del(Integer.valueOf(id));
        if (id.equals("1")){
            msg = "不能删除未分类";
        } else if (del == 1) {
            code = 0;
            msg = "删除成功";
            service.updId(Integer.valueOf(id));
        } else {
            msg = "删除失败";
        }
        jsonObject.put("code", code);
        jsonObject.put("msg", msg);
        jsonObject.put("data", null);

        out.print(jsonObject.toString());
    }
}
